<?php
class KomaSystem extends KomaComponent
{
    function main()
    {
        global $komaUserHandler;
        
        if(!$komaUserHandler->isUser()) redirect(KOMA_URL, T_('You have no permission to access this area'));
        
        switch($this->getParam('action', 'g'))
        {
            case 'account':
                $this->Database->query(sprintf("SELECT * FROM `%s` WHERE `uid`='%s'", $this->Database->prefix('users'), $komaUserHandler->getVar('uid')));
                
                if($row = $this->Database->fetchAssoc())
                {
                    $profileform = new KomaContainerForm('profileedit', $this->URL.'?action=profilesave');
                    $layoutprofile = new KomaLayoutForm();
                    $layoutprofile->add(new KomaWidgetText('name', $row['name'], null, array(new KomaRequiredFieldValidator('This filed cannot be empty')), array('style' => 'width:100%')), 'Name');
                    $layoutprofile->add(new KomaWidgetText('email', $row['email'], null, array(new KomaRequiredFieldValidator('This filed cannot be empty'), new KomaEmailValidator('Please fill with valid email address')), array('style' => 'width:100%')), 'Email');
                    $layoutprofile->add(new KomaWidgetText('url', $row['url'], null,array(new KomaRequiredFieldValidator('This filed cannot be empty'), new KomaURLValidator('Please fill with valid URL address')), array('style' => 'width:100%')), 'URL');
                    $layoutprofile->add(new KomaWidgetButton('saveprofile', 'Save', KOMA_BUTTON_SUBMIT));
                    $profileform->add($layoutprofile);
                    
                    $passform = new KomaContainerForm('changepassword', $this->URL.'?action=changepass');
                    $layoutpass = new KomaLayoutForm();
                    $layoutpass->add(new KomaWidgetPassword('oldpassword', null, array(new KomaRequiredFieldValidator('This filed cannot be empty')), array('style' => 'width:100%')), 'Current Password');
                    $layoutpass->add(new KomaWidgetPassword('newpassword1', null, array(new KomaRequiredFieldValidator('This filed cannot be empty'), new KomaMinLengthValidator(3, 'Passowrd lenght must more than 3 characters')), array('style' => 'width:100%')), 'New Password');
                    $layoutpass->add(new KomaWidgetPassword('newpassword2', null,array(new KomaRequiredFieldValidator('This filed cannot be empty'), new KomaCompareValidator('newpassword1', null, 'Please fill with same value with New Password')), array('style' => 'width:100%')), 'New Password (Re-type)');
                    $layoutpass->add(new KomaWidgetButton('savepassword', 'Save', KOMA_BUTTON_SUBMIT));
                    $passform->add($layoutpass);
                    
                    $this->Template->File = 'account.html';
                    $this->Template->assign('accountedit', new KomaLayoutStackPanel('stack', array('User Profile' => $profileform, 'Change Password' => $passform)));
                }
                
                break;
            case 'profilesave':
                if(!$this->getParams(array('name', 'email', 'url'))) redirect($this->URL.'?action=account', 'You must fill required field');
                
                if($this->Database->query(sprintf("UPDATE `%s` SET `name`='%s', `email`='%s', `url`='%s' WHERE `uid`='%s'", $this->Database->prefix('users'), $this->getParam('name'), $this->getParam('email'), $this->getParam('url'), $komaUserHandler->getVar('uid')))) redirect($this->URL.'?action=account', 'Success update user profile');
                redirect($this->URL.'?action=account', 'Failed update user profile');
                break;
            case 'changepass':
                if(!$this->getParams(array('oldpassword', 'newpassword1', 'newpassword2'))) redirect($this->URL.'?action=account', 'You must fill required field');
                if($this->getParam('newpassword1') != $this->getParam('newpassword2')) redirect($this->URL.'?action=account', 'New password you type did not match');
                
                $this->Database->query(sprintf("SELECT `password` FROM `%s` WHERE `uid`='%s' AND `password`='%s'", $this->Database->prefix('users'), $komaUserHandler->getVar('uid'), md5($this->getParam('oldpassword'))));
                
                if($row = $this->Database->fetchAssoc())
                {
                    if($this->Database->query(sprintf("UPDATE `%s` SET `password`='%s' WHERE `uid`='%s'", $this->Database->prefix('users'), md5($this->getParam('newpassword1')), $komaUserHandler->getVar('uid')))) redirect($this->URL.'?action=account', 'Success change password');
                    redirect($this->URL.'?action=account', 'Failed change password');
                }
                else redirect($this->URL.'?action=account', 'Current password your entered is wrong');
                break;
        }
    }
}
?>
